<?
abstract class WBase extends ANotify
{
	public $db;
	public $pagesize;
	public $page;
	public $start;
	public $total_record;
	////////////////////////////////////////////
	public $vrow;
	public $vadd;
	public $vedit;
	public $vcolspan;
	///////////////////////////////////////////
	protected $struct_url	= '';
	//////////////////////////////////////////
	protected $remark = "";
	//Add by peace 
	//2008-11-13
	protected $remark_view = "";
	protected $idx_div_window = array();
	protected $idx_field_name = "idx";
	//操作命令
	protected $cmd = 'list';
	function WBase($db,$pagesize = 20)
	{
		$this->db			= $db;
		$this->pagesize		= $pagesize;
		$this->Initialize();
		$this->vadd = false;
	}
	function Initialize()
	{
		$this->page			= 1;
		if(isset($_GET["page"]) && ereg("^[0-9]+$",$_GET["page"]))
			$this->page		= $_GET["page"];
		$this->start		= ($this->page - 1) * $this->pagesize;
		
		$this->total_record	= 0;
	}
	function RecordsetCount($sql)
	{
		$total				= 0;
		if(($query = $this->db->query($sql,MYSQL_NUM))==true)
		{
			if(($row = mysql_fetch_array($query))==true)
			{
				$this->total_record	= $row[0];
				$tPage		= ceil($this->total_record/$this->pagesize);
				if($this->page > $tPage && $tPage>0)
				{
					$this->page		= $tPage;
					$this->start	= ($this->page - 1) * $this->pagesize;
				}
			}
			else
				$this->halt("提取计算总数记录失败！");
		}
		else
			$this->halt("计算记录总数SQL语句错误！");
		return $total;
	}
	function pagenav()
	{
		$p			= new WPagenav;		//建立新对像


		$p->file	= $_SERVER["SCRIPT_NAME"];
		$p->pvar	= "page";	//设置页面传递的参数，默认为p	  
		$p->varstr  = $this->parse_QueryString('page');
		if($p->varstr != "")	$p->varstr = "&".$p->varstr; 
		$p->set($this->pagesize,$this->total_record);
		return $p->output(1);	
	}
	function pagenav_ajax()
	{
		$p			= new WPagenavAjax;		//建立新对像
		$p->js_name = "init_{$this->idx_field_name}";
		$p->set($this->pagesize,$this->total_record);
		return $p->output(1);	
	}
	function DoDefault()
	{
		$this->cmd = isset($_GET['cmd'])? strtolower($_GET['cmd']) : 'list';
		//print($this->cmd);exit;
		switch($this->cmd)
		{
			case "add":
				$this->wcms_log("进入新增 {$GLOBALS['USER_RIGHT']['TREE'][$GLOBALS['ATHENA_MODEL']]['NAME']} ");
				$this->Add();
				break;
			case "edit":
				$this->wcms_log("进入编辑 {$GLOBALS['USER_RIGHT']['TREE'][$GLOBALS['ATHENA_MODEL']]['NAME']} ");
				$this->Edit();
				break;
			case "delete":
				$len = isset($_POST['id']) ? count($_POST['id']) : 0;
				$this->wcms_log("删除 $len 条 {$GLOBALS['USER_RIGHT']['TREE'][$GLOBALS['ATHENA_MODEL']]['NAME']} 信息");
				$this->Del();
				break;
			case "save":
				if(isset($_POST['ID']) && $_POST['ID'] != 0)
					$this->wcms_log("修改 {$GLOBALS['USER_RIGHT']['TREE'][$GLOBALS['ATHENA_MODEL']]['NAME']} 信息");
				else 
					$this->wcms_log("新增 {$GLOBALS['USER_RIGHT']['TREE'][$GLOBALS['ATHENA_MODEL']]['NAME']} 信息");
				$this->Save();
				break;
			case "auditing":
				$this->wcms_log("审核 {$GLOBALS['USER_RIGHT']['TREE'][$GLOBALS['ATHENA_MODEL']]['NAME']} 信息");
				$this->Auditing();
				break;
			case "auditing_a":
				$this->wcms_log("批量审核 {$GLOBALS['USER_RIGHT']['TREE'][$GLOBALS['ATHENA_MODEL']]['NAME']} 信息");
				$this->Auditing_a();
				break;
			case "struct":
				$this->StructForm();
				break;
			case "struct_save":
				$this->StructForm_Save();
				break;
			case "struct_sort":
				$this->Struct_Sort();
				break;
			case "struct_sort_save":
				$this->Struct_Sort_Save();
				break;
			case "struct_view":
				$this->Struct_View();
				break;
			case "struct_view_save":
				$this->Struct_View_Save();
				break;
			case "view_ajax":
				$this->View_Ajax();
				break;
			case "repeat":
				$this->Check_Table_Primary_Repeat();
				break;
			case "mselect":
				$this->MSelect_child();
				break;
			case "foreign":
				$this->Foreign_Select();
				break;
			case "maxidx":
				$this->Initialize_Max_Idx();
				echo $this->getMax_Idx();exit;
				break;
			default:
				$this->wcms_log("查看 {$GLOBALS['USER_RIGHT']['TREE'][$GLOBALS['ATHENA_MODEL']]['NAME']} 信息");
				$this->cmd	= "list";
				$this->View();
				break;
		}
	}
	function View()
	{
		set_cookie("SYSTEM_HTTP_REFERER",$_SERVER['REQUEST_URI']);
		if(isset($_COOKIE["WADD"]))
		{
			while (list($key,) = each($_COOKIE['WADD'])) 
			{
				setcookie("WADD[$key]","",time()-3600);	
			}
		}
		?>
			<table Class="yc" cellspacing="1" cellpadding="0">
			  <?
			  	$this->View_Data();print($this->View_List_Header());print($this->View_List());
				print("<tr><td colspan=\"{$this->vcolspan}\" height=\"21\" style=\"text-align:center;\">".$this->pagenav()."</td></tr>");
				$this->View_Button();
				?>	
			</table>
			<table Class="yc" cellspacing="1" cellpadding="0">
			<?print($this->Print_Search_Form());?>
			</table>
			<table Class="yc" cellspacing="1" cellpadding="0">
			<?=$this->remark_view== ""?"":"<tr><td >{$this->remark_view}</td></tr>"?>
			</table>
		<br>
		<?
		unset($this->vrow);
	}
	//Add by peace@warmy.cn
	//为xmlHTTP提供数据
	//Create Date 2008-11-13
	public function View_Ajax()
	{
		return "";	
	}
	abstract public function View_Data();
	abstract function View_List_Header();
	abstract function View_List();

	function View_Button()
	{
		print("<tr bgcolor=\"#FFFFFF\"><td colspan=\"{$this->vcolspan}\" height=\"50\" style=\"text-align:center\">");
		if($this->vadd && (check_manage_right_micro($GLOBALS['ATHENA_MODEL'],ATHENA_CMD_ADD) == ATHENA_CMD_ADD))
			print("<input name=\"Add\" type=\"Submit\" class=\"button\" value=\"新增\" onclick=\"AppendCmd('add')\">");
	
		if(check_manage_right_micro($GLOBALS['ATHENA_MODEL'],ATHENA_CMD_AUDITING) == ATHENA_CMD_AUDITING)
			print("&nbsp;<input name=\"Auditing\" type=\"Submit\" class=\"button\" value=\"批量审核\" onclick=\"if(window.confirm('确信批量审核？')){AppendCmd('auditing_a')}else{return false;}\">");
			
		if(($this->vadd || $this->vedit) && check_manage_right_micro($GLOBALS['ATHENA_MODEL'],ATHENA_CMD_DELETE) == ATHENA_CMD_DELETE)
			print("&nbsp;<input name=\"Delete\" type=\"Submit\" class=\"button\" value=\"删除\" onclick=\"if(window.confirm('确信删除？')){AppendCmd('delete')}else{return false;}\">");
		print("</td></tr>");
	}
	//
	function Add()
	{
		$backurl = get_cookie("SYSTEM_HTTP_REFERER");
		?>
		<table Class="yc" cellspacing="1" cellpadding="0">
			<?$this->Add_Form();?>
			<tr bgcolor="#FFFFFF">
				<td colspan="2" height="50" style="text-align:center">
					<?if($this->cmd == "add" 
					&& (check_manage_right_micro($GLOBALS['ATHENA_MODEL'],ATHENA_CMD_ADD) == ATHENA_CMD_ADD))
					print("<input name=\"Submit\" type=\"submit\" class=\"button\" value=\"保存\" onclick=\"AppendCmd('save')\">");
					?>
					<input name="back" type="submit" class="button" value="返回" onclick="AppendCmd('back')"/>
					<input type="hidden" name="SYSTEM_HTTP_REFERER" value="<?=$backurl?>">				
				</td>
			</tr>
			<?if($this->remark != "") print("<tr><td colspan=2>{$this->remark}</td></tr>");?>
		</table>
		<br>
		<?$this->CheckForm();?>
		<?
	}
	function Edit()
	{
		$backurl = get_cookie("SYSTEM_HTTP_REFERER");
		$id		 = 0;
		if(isset($_GET["id"]) && ereg("^[0-9]+$",$_GET["id"]))
			$id		= $_GET["id"];
		else
			$this->halt("没有选择需要编辑的记录",'ERR');
		?>
			<table Class="yc" cellspacing="1" cellpadding="0">
			  <?$this->Add_Form($id);?>
			   <tr bgcolor="#FFFFFF">
				<td colspan="2" height="50" style="text-align:center">
					<?$this->Edit_Button();?>
					<input type="hidden" name="SYSTEM_HTTP_REFERER" value="<?=$backurl?>">
					<input type="hidden" name="ID" value="<?=$id?>">
				</td>
			  </tr>
			  <?if($this->remark != "") print("<tr><td colspan=2>{$this->remark}</td></tr>");?>
			</table>
		<br>
		<? $this->CheckForm();
	}
	function Edit_Auditing($val)
	{
		if($GLOBALS['cmd'] == "edit" && ($GLOBALS['user_right']->checkright($GLOBALS['Athena_Model'],ATHENA_CMD_AUDITING) == ATHENA_CMD_AUDITING))
		{
			if($val<0 || $val>2)
				$val = 0;
			print("<tr><td style=\"text-align:center;\">信息审核 &nbsp;</td><td>");
			print("<input type=\"radio\" name=\"athena_autiding\" value=\"1\" ");
			print($val == 1 ?"checked":"");
			print(">通过 ");
			print("<input type=\"radio\" name=\"athena_autiding\" value=\"2\" ");
			print($val == 2 ?"checked":"");
			print(">退回 ");
			print("<input type=\"radio\" name=\"athena_autiding\" value=\"0\" ");
			print($val == 0 ?"checked":"");
			print(">待审 ");
			print("&nbsp;&nbsp;&nbsp;&nbsp;<font color=red>信息审核员不可编辑信息</font></td></tr>");
		}
	}
	function Edit_Button()
	{
		if($this->cmd == "edit" && (check_manage_right_micro($GLOBALS['ATHENA_MODEL'],ATHENA_CMD_EDIT) == ATHENA_CMD_EDIT))
			print("<input name=\"Submit\" type=\"submit\" class=\"button\" value=\"保存\" onclick=\"AppendCmd('save')\">&nbsp;");
		if($this->cmd && (check_manage_right_micro($GLOBALS['ATHENA_MODEL'],ATHENA_CMD_AUDITING) == ATHENA_CMD_AUDITING))
			print("<input name=\"auditing\" type=\"submit\" class=\"button\" value=\"审核\" onclick=\"AppendCmd('auditing')\">&nbsp;");
		print("<input name=\"Back\" type=\"submit\" class=\"button\" value=\"返回\" onclick=\"AppendCmd('back')\">");
	}
	abstract function Add_Form();
	
	function CheckForm()
	{
		;
	}
	function Save()
	{
		if(isset($_POST["ID"]) && ereg("^[0-9]+$",$_POST["ID"]))
		{
			if(check_manage_right_micro($GLOBALS['ATHENA_MODEL'],ATHENA_CMD_EDIT) != ATHENA_CMD_EDIT)
				$this->halt("您不具有访问该功能的权限！",ATHENA_SCRIPT_INDEX);
			$this->Save_Update($_POST["ID"]);
		}
		else
		{
			if(check_manage_right_micro($GLOBALS['ATHENA_MODEL'],ATHENA_CMD_ADD) != ATHENA_CMD_ADD)
				$this->halt("您不具有访问该功能的权限！",ATHENA_SCRIPT_INDEX);
			$this->Save_Add();
		}

	}
	function Auditing()
	{
		if(isset($_GET["id"]) && ereg("^[0-9]+$",$_GET["id"]))
		{
			$auditing = 0;
			if(isset($_POST['athena_autiding']) && ereg("^[0-2]{1}$",$_POST['athena_autiding']))
				$auditing	= $_POST['athena_autiding'];
			$sql = "update `{$this->table}` set `auditing`=$auditing where `{$this->primary}` = {$_GET["id"]}";
			//print($sql);exit;
			$this->db->query($sql);
		}
		else 
			$this->halt("请选择需要审核的内容！");
		$this->halt("审核已提交",$_COOKIE["SYSTEM_HTTP_REFERER"]);
	}
	function Auditing_a()
	{
		if(isset($_POST["id"]))
		{
			$str	= implode(",",$_POST["id"]);
			if(ereg("^[0-9,]+$",$str))
			{
				$sql = "update `{$this->table}` set `auditing`= case when  `auditing`=0 then 1  when `auditing`=1  then 0  else `auditing` end where `{$this->primary}` in ($str)";
				$this->db->query($sql);
				$this->halt("成功批量审核");
			}
			else 
				$this->halt("错误删除参数！");
		}
		else 
			$this->halt("没有选择需要批量审核的记录");
	}
	private function StructForm()
	{
		if($this->Check_Struct_Exists())
			$struct = $this->Load('array');
		else 
		{
			//需要增加代码
			$struct	= $this->updatestring($this->struct);
		}
		$str	= "";

		?>
		<table class="AthenaManage" >
		  <tr>
			<td Id="ManageTip">属性更改</td>
			<td Id="ManageContent"></td>
		  </tr>
		  <tr>
			<td colspan="2" Id="ManageBg">
			<table  border="0" cellpadding="0" cellspacing="1" >
			  <tr><td height="8"></td></tr>
			</table>
			<table Class="AthenaManageTable" Id="AthenaManageTable">
			<form name="frmList" method="POST" action="<?=$_SERVER['REQUEST_URI']?>">
			<?
		if(isset($struct["publish"]) && is_array($struct["publish"]))
		{
			$value	= isset($struct["publish"]['template_dir'])?$struct["publish"]['template_dir']:"";
			print("<tr><td rowspan=\"5\" valign=\"top\">publish</td>");
			print ("<td>template_dir</td><td><input type=\"text\" name=\"template_dir\" class=\"INPUTTEXT\" value=\"$value\"></td></tr>");
			$value	= isset($struct["publish"]['template_name'])?$struct["publish"]['template_name']:"";
			print ("<tr><td>内容模板名称</td><td><input type=\"text\" name=\"template_name\" class=\"INPUTTEXT\" value=\"$value\"></td></tr>");
			$value	= isset($struct["publish"]['template_dump_path'])?$struct["publish"]['template_dump_path']:"";
			print ("<tr><td>栏目路径</td><td><input type=\"text\" name=\"template_dump_path\" class=\"INPUTTEXT\" value=\"$value\"></td></tr>");
			$value	= isset($struct["publish"]['assign'])?$struct["publish"]['assign']:"";
			print ("<tr><td>替换参数列表</td><td><input type=\"text\" name=\"assign\" class=\"INPUTTEXT\" value=\"$value\"></td></tr>");
			$value	= isset($struct["publish"]['template_list_name'])?$struct["publish"]['template_list_name']:"";
			print ("<tr><td>栏目模板页面</td><td><input type=\"text\" name=\"template_list_name\" class=\"INPUTTEXT\" value=\"$value\"></td></tr>");
		}
		while(list(,$value) = each($struct['fields']))
		{
			if($value['Field_View'])
				print($this->StructForm_Item($value));
		}
		print($str);
		?>
		<tr><td colspan="3" style="text-align:center;"><input name="delete" type="image" src="images/save.jpg" width="78" height="25" border="0" onclick="AppendCmd('struct_save')"></td></tr>
		</form>
		</table></td>
		  </tr>
		  <tr>
			<td colspan="2" Id="ManageBottom">&nbsp;</td>
		  </tr>
		</table>
		<?
	}
	private function StructForm_Item($values)
	{
		$str	= "";
		$str	= "$str<tr><td valign=\"top\" style=\"width:100px;\" rowspan=\"17\">{$values['Field_Name']}</td>
					<td style=\"width:160px;background-color:#CADEDF\">Form_Name</td>
					<td ><input type=\"text\" name=\"{$values['Field_Name']}_Form_Name\" class=\"INPUTTEXT\" value=\"{$values['Form_Name']}\"></td></tr>";
		$str	= "$str<tr>
						<td style=\"background-color:#EFF3EF\">Form_Desc</td>
						<td ><input type=\"text\" name=\"{$values['Field_Name']}_Form_Desc\" class=\"INPUTTEXT\" value=\"".htmlspecialchars($values['Form_Desc'],ENT_QUOTES)."\"></td>
					</tr>";
		$str	= "$str<tr>
						<td style=\"background-color:#CADEDF\">Form_Type</td>
						<td >".$this->StructForm_Item_Select(array("text","date","hidden","password","textarea","select","radio","checkbox","file"),$values['Field_Name']."_Form_Type",$values['Form_Type'])."</td>
					</tr>";
		$str	= "$str<tr>
						<td style=\"background-color:#EFF3EF\">Form_Editor</td>
						<td >".$this->StructForm_Item_checkbox($values['Field_Name']."_Form_Editor",$values['Form_Editor'])."</td>
					</tr>";
		$str	= "$str<tr>
						<td style=\"background-color:#CADEDF\">Form_Default</td>
						<td ><input type=\"text\" name=\"{$values['Field_Name']}_Form_Default\" class=\"INPUTTEXT\" value=\"".htmlspecialchars($values['Form_Default'],ENT_QUOTES)."\"></td>
					</tr>";
		$str	= "$str<tr>
						<td style=\"background-color:#EFF3EF\">Form_CSS</td>
						<td ><input type=\"text\" name=\"{$values['Field_Name']}_Form_CSS\" class=\"INPUTTEXT\" value=\"".htmlspecialchars($values['Form_CSS'],ENT_QUOTES)."\"></td>
					</tr>";
		$str	= "$str<tr>
						<td style=\"background-color:#CADEDF\">Form_Maxlength</td>
						<td ><input type=\"text\" name=\"{$values['Field_Name']}_Form_Maxlength\" class=\"INPUTTEXT\" value=\"".htmlspecialchars($values['Form_Maxlength'],ENT_QUOTES)."\"></td>
					</tr>";
		$str	= "$str<tr>
						<td style=\"background-color:#EFF3EF\">Form_View</td>
						<td >".$this->StructForm_Item_checkbox($values['Field_Name']."_Form_View",$values['Form_View'])."</td>
					</tr>";
		//$str	= "$str<tr>
		//				<td style=\"background-color:#CADEDF\">Field_Name</td>
		//				<td ><input type=\"text\" name=\"{$values['Field_Name']}_Field_Name\" class=\"INPUTTEXT\" value=\"title\"></td>
		//			</tr>";
		$str	= "$str<tr>
						<td style=\"background-color:#EFF3EF\">Field_Optional</td>
						<td >".$this->StructForm_Item_checkbox($values['Field_Name']."_Field_Optional",$values['Field_Optional'])."</td>
					</tr>";
		$str	= "$str<tr>
						<td style=\"background-color:#CADEDF\">Field_Pattern</td>
						<td ><input type=\"text\" name=\"{$values['Field_Name']}_Field_Pattern\" class=\"INPUTTEXT\" value=\"".htmlspecialchars($values['Field_Pattern'],ENT_QUOTES)."\"></td>
					</tr>";
		$str	= "$str<tr>
						<td style=\"background-color:#EFF3EF\">Field_Alert</td>
						<td ><input type=\"text\" name=\"{$values['Field_Name']}_Field_Alert\" class=\"INPUTTEXT\" value=\"".htmlspecialchars($values['Field_Alert'],ENT_QUOTES)."\"></td>
					</tr>";
		$str	= "$str<tr>
						<td style=\"background-color:#CADEDF\">Field_Option_Sql</td>
						<td ><input type=\"text\" name=\"{$values['Field_Name']}_Field_Option_Sql\" class=\"INPUTTEXT\" value=\"".htmlspecialchars($values['Field_Option_Sql'],ENT_QUOTES)."\"></td>
					</tr>";
		$str	= "$str<tr>
						<td style=\"background-color:#EFF3EF\">Field_View</td>
						<td >".$this->StructForm_Item_checkbox($values['Field_Name']."_Field_View",$values['Field_View'])."</td>
					</tr>";
		$str	= "$str<tr>
						<td style=\"background-color:#CADEDF\">Field_Add</td>
						<td >".$this->StructForm_Item_checkbox($values['Field_Name']."_Field_Add",$values['Field_Add'])."</td>
					</tr>";
		$str	= "$str<tr>
						<td style=\"background-color:#EFF3EF\">Field_Edit</td>
						<td >".$this->StructForm_Item_checkbox($values['Field_Name']."_Field_Edit",$values['Field_Edit'])."</td>
					</tr>";
		$str	= "$str<tr>
						<td style=\"background-color:#CADEDF\">IsSeach</td>
						<td >".$this->StructForm_Item_checkbox($values['Field_Name']."_IsSeach",$values['IsSeach'])."</td>
					</tr><tr><td colspan=\"2\">&nbsp;</td></tr>";
		return $str;
	}
	private function StructForm_Item_checkbox($name,$value)
	{
		$retval	= "<input type=\"radio\" name=\"$name\" value=\"1\" ";
		if($value)
			$retval	= "$retval checked";
		$retval	= "$retval>是 &nbsp;<input type=\"radio\" name=\"$name\" value=\"0\" ";
		if(!$value)
			$retval	= "$retval checked";
		$retval	= "$retval>否 ";
		return $retval;
	}
	private function StructForm_Item_Select($arr,$name,$value)
	{
		$retval	= "";
		$len	= count($arr);
		$retval	= "$retval<select name=\"$name\" style=\"width:100px;\">";
		for($i=0;$i<$len;$i++)
		{
			if(strtolower($arr[$i]) == strtolower($value))
				$selected = " selected";
			else 
				$selected = "";
			$retval = "$retval<option value=\"{$arr[$i]}\" $selected>{$arr[$i]}</option>";
		}
		$retval	= "$retval</select>";
		return $retval;
	}
	private function StructForm_Save()
	{
		if($this->struct_url != "")
		{
			$xml	= $this->Load();
			$struct	= $xml->createArray();
			//$struct = $this->Load('array');
			if(!isset($struct['struct']) || !is_array($struct['struct']))
				$this->halt('配置文件加载失败！');
			else 
				$struct	= $struct['struct'];
		}
		else 
			$struct	= array("fields"=>$this->struct);
		if(isset($struct["publish"]) && is_array($struct["publish"]))
		{
			$key	= "template_dir";
			if(isset($_POST[$key]))
				$struct["publish"][$key] = stripslashes($_POST[$key]);
			elseif(isset($struct["publish"][$key]))
				$struct["publish"][$key] = "";
			$key	= "template_name";
			if(isset($_POST[$key]))
				$struct["publish"][$key] = stripslashes($_POST[$key]);
			elseif(isset($struct["publish"][$key]))
				$struct["publish"][$key] = "";
			$key	= "template_dump_path";
			if(isset($_POST[$key]))
				$struct["publish"][$key] = stripslashes($_POST[$key]);
			elseif(isset($struct["publish"][$key]))
				$struct["publish"][$key] = "";
			$key	= "assign";
			if(isset($_POST[$key]))
				$struct["publish"][$key] = stripslashes($_POST[$key]);
			elseif(isset($struct["publish"][$key]))
				$struct["publish"][$key] = "";
			$key	= "template_list_name";
			if(isset($_POST[$key]))
				$struct["publish"][$key] = stripslashes($_POST[$key]);
			elseif(isset($struct["publish"][$key]))
				$struct["publish"][$key] = "";
		}
		foreach ($struct['fields'] as $key=>$value)
		{
			if($value['Field_View'])
			{
				$struct['fields'][$key]['Form_Name']	= $this->StructForm_Save_Text($value['Field_Name'],"Form_Name");
				$struct['fields'][$key]['Form_Desc']	= $this->StructForm_Save_Text($value['Field_Name'],"Form_Desc");
				$struct['fields'][$key]['Form_Type']	= $this->StructForm_Save_Text($value['Field_Name'],"Form_Type");
				$struct['fields'][$key]['Form_Default']	= $this->StructForm_Save_Text($value['Field_Name'],"Form_Default");
				$struct['fields'][$key]['Form_CSS']	= $this->StructForm_Save_Text($value['Field_Name'],"Form_CSS");
				$struct['fields'][$key]['Form_Maxlength']	= $this->StructForm_Save_Text($value['Field_Name'],"Form_Maxlength");
				$struct['fields'][$key]['Field_Option_Sql']	= $this->StructForm_Save_Text($value['Field_Name'],"Field_Option_Sql");
				$struct['fields'][$key]['Field_Pattern']	= $this->StructForm_Save_Text($value['Field_Name'],"Field_Pattern");
				$struct['fields'][$key]['Field_Alert']	= $this->StructForm_Save_Text($value['Field_Name'],"Field_Alert");
				$struct['fields'][$key]['Form_Editor']	= $this->StructForm_Save_CheckBox($value['Field_Name'],"Form_Editor");
				$struct['fields'][$key]['Form_View']	= $this->StructForm_Save_CheckBox($value['Field_Name'],"Form_View");
				$struct['fields'][$key]['Field_Optional']	= $this->StructForm_Save_CheckBox($value['Field_Name'],"Field_Optional");
				$struct['fields'][$key]['Field_View']	= $this->StructForm_Save_CheckBox($value['Field_Name'],"Field_View");
				$struct['fields'][$key]['Field_Add']	= $this->StructForm_Save_CheckBox($value['Field_Name'],"Field_Add");
				$struct['fields'][$key]['Field_Edit']	= $this->StructForm_Save_CheckBox($value['Field_Name'],"Field_Edit");
				$struct['fields'][$key]['IsSeach']	= $this->StructForm_Save_CheckBox($value['Field_Name'],"IsSeach");
			}
		}
		if($this->struct_url != "")
		{
			$array	= new WArray2Xml("struct");
			$xml->xml = $array->array2xml($struct);
			$xml->save(dirname($this->struct_url) . DIR_SEP . STRUCT_NAME);
		}
		else 
		{
			$this->Struct_Save_PHP($struct);;
		}
		$this->halt("设置字段属性成功！",$_COOKIE["SYSTEM_HTTP_REFERER"] . "&cmd=struct");
		//print_r($this->struct);
		//header("Location: ". $_COOKIE["SYSTEM_HTTP_REFERER"]);
		exit;
	}
	private function StructForm_Save_Text($field,$name)
	{
		$key	= "{$field}_{$name}";
		if(isset($_POST[$key]))
			return stripslashes($_POST[$key]);
		else 
			return "";
	}
	private function StructForm_Save_CheckBox($field,$name)
	{
		$value	= 0;
		$key	= "{$field}_{$name}";
		if(isset($_POST[$key]))
		{
			if($_POST[$key] == "1")
				$value = 1;
		}
		return $value;
	}
	private function updatestring($array){
		$dir = DOCUMENTROOT.MANAGE_VIRTUAL.'template/'.dirname($GLOBALS['Athena_HREF'])."/struct.php";
		$fb	 = new WFile();
		$content = $fb->getfile($dir);
		$struct = array();
		$pos = 0;
		foreach($array as $key => $val){
				$startpos = strpos($content,$key,$pos);
				$content = substr($content,$startpos);
				$str = preg_split("/\t\\),/",$content,2);
				$pos = strlen($str[0]);
				if(is_array($val)){
					$matches = array();
					reset($val);
					//foreach($val as $k =>$v)
					while(list($k,)=each($val))
					{
						preg_match("/('|\")".$k."\\1(\\s*\\=>\\s*)(\"|')?(.*)((\"|')?\\s*(,*)\\s*(\\/\\/.*)*)\n/ieU",$str[0],$matches);
						$match = trim($matches[4]);
						if(preg_match("/^(\"|')/ieU",$match)){
							$match = substr($match,1);
						}
						$val[$k] = $match;
					}
				}
				$struct['fields'][$key] = $val;
			}
		return $struct;
	}
	private function Struct_View()
	{
		
		//if($this->Check_Struct_Exists())
		//	$struct = $this->Load('array');
		//else 
		//	$struct	= $this->updatestring($this->struct);
		?>
		<table class="AthenaManage" >
		  <tr>
			<td Id="ManageTip">显示字段</td>
			<td Id="ManageContent"></td>
		  </tr>
		  <tr>
			<td colspan="2" Id="ManageBg">
			<table  border="0" cellpadding="0" cellspacing="1" >
			  <tr><td height="8"></td></tr>
			</table>
			<table Class="AthenaManageTable" Id="AthenaManageTable">
			 <form name="frmList" method="POST" action="<?=$_SERVER['REQUEST_URI']?>">
			<?
				$str	= "";
				foreach ($this->struct as $key=>$value)
				{
					if($value['Field_View'])
						$checked	= " checked";
					else 
						$checked	= "";
					$str	= "$str<tr><td style=\"width:160px\">{$value['Form_Name']}</td><td><input type=\"checkbox\" name=\"$key\" value=\"1\" $checked></td></tr>";
				}
				print($str);
			?>
			<tr><td colspan="2" style="text-align:center;"><input name="delete" type="image" src="images/save.jpg" width="78" height="25" border="0" onclick="AppendCmd('struct_view_save')"></td></tr>
			</form></table></td>
		  </tr>
		  <tr>
			<td colspan="2" Id="ManageBottom">&nbsp;</td>
		  </tr>
		</table>
		<?
	}
	private function Struct_View_Save()
	{
		if($this->struct_url != "")
		{
			$xml	= $this->Load();
			$struct	= $xml->createArray();
			//$struct = $this->Load('array');
			if(!isset($struct['struct']) || !is_array($struct['struct']))
				$this->halt('配置文件加载失败！');
			else 
				$struct	= $struct['struct'];
		}
		else
		{ 
			$struct = $this->updatestring($this->struct);
		}
		
		while(list($key,) = each($struct['fields']))
		{//print($_POST[$key]);
			if(isset($_POST[$key]))
				$struct['fields'][$key]['Field_View'] = 1;
			else 
				$struct['fields'][$key]['Field_View'] = 0;
		}
		if($this->struct_url != "")
		{
			$array	= new WArray2Xml("struct");
			$xml->xml = $array->array2xml($struct);
			$xml->save(dirname($this->struct_url) . DIR_SEP . STRUCT_NAME);
		}
		else 
		{
			$this->Struct_Save_PHP($struct);;
		}
		$this->halt("重置显示字段成功",$_COOKIE["SYSTEM_HTTP_REFERER"] . "&cmd=struct_view");
	}
	private function Check_Struct_Exists()
	{
		$retval	= true;
		//判断配置文件是否存在
		if(!defined('STRUCT_NAME') || !defined('DIR_SEP'))
			$this->View_List();
		$path	= dirname($this->struct_url) . DIR_SEP . STRUCT_NAME;
		if(!file_exists($path))
			$retval	= false;
		return $retval;
	}
	private function Load($type='')
	{
		$type	= strtolower($type);
		$path	= dirname($this->struct_url) . DIR_SEP . STRUCT_NAME;
		$xml	= new WXml2Array();
		$xml->load($path);
		if($type == "xml")
			return $xml->xml;
		elseif($type == "array")
		{
			$struct	= $xml->createArray();
			if(!isset($struct['struct']) || !is_array($struct['struct']))
				$this->halt('配置文件加载失败！');
			else 
				$struct	= $struct['struct'];
			return $struct;
		}
		else 
			return $xml;
		
	}
	private function Struct_Sort()
	{//ob_clean();
		//print_R($this->struct);exit;
		//if($this->struct_url == "")
		//	$this->struct = array("fields"=>$this->struct);
		?>
		<table class="AthenaManage" >
		  <tr>
			<td Id="ManageTip">显示字段</td>
			<td Id="ManageContent"></td>
		  </tr>
		  <tr>
			<td colspan="2" Id="ManageBg">
			<table  border="0" cellpadding="0" cellspacing="1" >
			  <tr><td height="8"></td></tr>
			</table>
			<table Class="AthenaManageTable" Id="AthenaManageTable">
			 <form name="frmList" method="POST" action="<?=$_SERVER['REQUEST_URI']?>">
			<?
				$str	= "";
				$i		= 1;//
				reset($this->struct);
				while(list(,$value) = each($this->struct))//foreach ($this->struct as $key=>$value)
				{
					$str	= "$str<tr><td style=\"width:160px\">{$value['Form_Name']}</td><td style=\"width:160px\">{$value['Form_Desc']}</td><td><input type=\"text\" name=\"{$value['Field_Name']}\" style=\"width:50px\" value=\"$i\"></td></tr>";
					$i++;
				}
				print($str);
			?>
			<tr><td colspan="2" style="text-align:center;"><input name="delete" type="image" src="images/save.jpg" width="78" height="25" border="0" onclick="AppendCmd('struct_sort_save')"></td></tr>
			</form></table></td>
		  </tr>
		  <tr>
			<td colspan="2" Id="ManageBottom">&nbsp;</td>
		  </tr>
		</table>
		<?
	}
	private function Struct_Sort_Save()
	{
		if($this->struct_url != "")
		{
			$xml	= $this->Load();
			$struct	= $xml->createArray();
			//$struct = $this->Load('array');
			if(!isset($struct['struct']) || !is_array($struct['struct']))
				$this->halt('配置文件加载失败！');
			else 
				$struct	= $struct['struct'];
		}
		else 
		{
			$struct = $this->updatestring($this->struct);
		}
		$arr = array();
		foreach ($struct['fields'] as $key=>$value)
		{
			if($_POST[$key] && ereg("^[0-9]+$",$_POST[$key]))
				$arr[$key]	= $_POST[$key];
			else 
				$arr[$key]	= 0;
		}
		$arr	= array_flip($arr);
		ksort($arr);
		$fields	= array();
		foreach($arr as $key=>$value)
			$fields[$value]	= $struct["fields"][$value];
		if($this->struct_url != "")
		{
			$array	= new WArray2Xml("fields");
			$fields = $array->array2xml($fields);
			$xml->xml	= ereg_replace("<fields>.+</fields>",$fields,$xml->xml);
			$xml->save(dirname($this->struct_url) . DIR_SEP . STRUCT_NAME);
		}
		else 
		{
			$this->Struct_Save_PHP(array("fields"=>$fields));
			//print_r($struct);
			//exit;
		}
		$this->halt("调整显示字段成功",$_COOKIE["SYSTEM_HTTP_REFERER"] . "&cmd=struct_sort");
	}
	private function Struct_Save_PHP($array)
	{
		if(is_array($array)){
			$fb	= new WFile();
			$string = '';
			$dir = DOCUMENTROOT.MANAGE_VIRTUAL.'template/'.dirname($GLOBALS['Athena_HREF'])."/struct.php";
			$content = $fb->getfile($dir);
			$pos = strpos($content,"\"fields\"");
			$content = substr($content,0,$pos);
			$string = $content.stripcslashes($this->Struct_Save_PHP_Array($array)).");\n?>";
			$fb->write_to_file($dir,$string,"w");
		}
	}
	private function Struct_Save_PHP_Array($array,$level=0)
	{
		if(!is_array($array)){
			return $array;
		}
		$space = "";
		for($i = 0; $i < $level; $i++) {
			$space .= "\t";
		}
		$evaluate = ($level == 0) ? "" : "Array(\n";
		$comma = $space;
		if(is_array($array)){
			foreach($array as $key => $val) {
				$key = is_string($key) ? '"'.addcslashes($key, '"\\').'"' : $key;
				$val = !is_array($val) && (!preg_match("/(^\\-?[1-9]\\d*$)|(true|false)|(^(.*)\\(.*\\))/", $val)) ? '"'.addcslashes($val, '"\\').'"' : $val;
				if(is_array($val)) {
					$evaluate .= "$comma$key => ".$this->Struct_Save_PHP_Array($val, $level + 1);
				} else {
					$evaluate .= ($key == '"Form_Default"' || $key == '"Form_Maxlength"' ) ?  "$comma$key => ".$val : "$comma$key => ".($val != 1 ? $val : ($val ? 'true' : 'false')) ;
				}
				$comma = ",\n$space";
			}
		}
		$evaluate .= ($level == 0) ? "\n$space" : "\n$space)" ;
		return $evaluate;
	}
	function __set($key,$val)
	{
		switch($key)
		{
			case "remark":
				$this->remark = $val;
				break;
			case "remark_view":
				$this->remark_view = $val;
		}
	}

	function __destruct()
	{
		while (list(,$value) = each($this->idx_div_window)){
			print($value);
		}
		if(count($this->idx_div_window)>0)
		{
			$str = "<script language=\"javascript\">function select_idx(textid){
				try
				{
					var obj = document.getElementsByTagName('INPUT')
					var len = obj.length;
					var idx = -1;
					for(i=0;i<len;i++)
					{
						if(obj[i].type !='radio')
							continue;
						if(obj[i].name == 'widx_sort')
						{
							if(obj[i].checked)
							{
								idx = i;
								break;
							}
						}
					}
					if(idx == -1)
					{
						alert('请选择排序参考纪录！');
					}
					obj1 = eval('document.frmList.' + textid + '_radio');
					if(obj1[0].checked)
						idx = parseInt(obj[idx].value)-1;
					else
						idx = parseInt(obj[idx].value)+1;
					obj =  eval('document.frmList.' + textid);
					obj.value = idx; 
					closeDiv('floatdiv_' + textid);
				}
				catch(e)
				{}
			}
			function BeginSearch_idx(frm)
			{
				var len = frm.length;
				str = \"\";
				for(i=0;i<len;i++)
				{
					var s = false;
					try
					{
						if(frm[i].IsSearch == \"1\")
							s = true;
					}
					catch(e)
					{
						;
					}
					if(s)
					{
						str += \"&\" + frm[i].name + \"=\" + frm[i].value.UrlEncode();
					}
				}
				window.where = (str);
				init_{$this->idx_field_name}(1);
			}
			</script>";
			print($str);
		}
	}
	//设置排序字段的值
	function idx_field_nameSet($value)
	{
		$this->idx_field_name = $value;
	}
	/**
	 * 纪录当前操作日志
	 *
	 */
	function wcms_log($op)
	{
		$username 	= isset($_SESSION['REEYEE_MANAGE']['user']) && $_SESSION['REEYEE_MANAGE']['user'] != '' ? $_SESSION['REEYEE_MANAGE']['user'] : '匿名';
		$username	= isset($_SESSION['REEYEE_MANAGE']['realname']) && $_SESSION['REEYEE_MANAGE']['realname']!= '' ? "{$username}（{$_SESSION['REEYEE_MANAGE']['realname']}）":$username;
		$active		= date("Y-m-d H:i:s");
		$ip			= ip();
		$sql = "insert into ".WWCMSLOGTABLE."(username,op,active,ip) values('$username','$op','$active','$ip')";
		$this->db->query($sql);
	}
	
	function Check_Table_Primary_Repeat()
	{
		//遍历所有字段，如果有字段是text_primary类型，则判断GE值是否存在
		$struct = $this->struct;
		while (list($key,$value) = each($struct)) {
			if($value['Form_Type'] == 'text_primary' && isset($_POST[$key]) && $_POST[$key] != '')
			{
				//表单类型存在，且GET方式提交了值，
				$sql = "select `{$this->primary}` from `{$this->table}` where `$key` = '{$_POST[$key]}'";
				$tmp = $this->db->get_one($sql);
				if($tmp)
				{
					print("{$value['Form_Desc']} {$_POST[$key]} 已存在！");
				}
			}
		}
		exit;
	}
	function MSelect_child()
	{
		//遍历所有字段，如果有字段是text_primary类型，则判断GE值是否存在
		$struct = $this->struct;
		while (list($key,$value) = each($struct)) {
			if($value['Form_Type'] == 'mselect' && isset($_POST[$key]) && $_POST[$key] != '')
			{
				$sql = $struct[$value['Form_Editor']]['Field_Option_Sql'];
				$where = " where `$key` = $_POST[$key] ";
				$sql = str_replace("{where}",$where,$sql);
				$list = $this->db->query_row($sql,MYSQL_NUM);
				$str = "";
				while (list(,$value) = each($list)) {
					$str = "{$str}|||{$value[0]}|||{$value[1]}";
				}
				if($str!='')
					$str = $str = substr($str,3);
				print($str);
			}
		}
		exit;
	}
	function Foreign_Select()
	{
		//遍历所有字段，如果有字段是text_primary类型，则判断GE值是否存在
		$struct = $this->struct;
		while (list($key,$value) = each($struct)) {
			if($value['Form_Type'] == 'ijselect' && isset($_POST[$key]) && $_POST[$key] != '')
			{
				$str = $struct[$key]['Form_Editor'];
				$arr = split("\|",$str);
				$sql = "select `{$arr[0]}`,".(isset($arr[3]) ? $arr[3] : "`{$arr[1]}`") . " as {$arr[1]} from `{$arr[2]}` where `{$arr[1]}` like '{$_POST[$key]}%' ";
				$list = array();
				if(($query = mysql_query($sql))==true)
				{
					while ($row = mysql_fetch_array($query,MYSQL_ASSOC)) {
						$list[] = $row;
					}
				}
				
				$str = "";
				$xml = new WArray2Xml('warmy');
				$str = $xml->array2xml($list);
				print($str);
			}
		}
		exit;
	}
	function Foreign_exists($key,$value)
	{
		$sql = "select `{$this->join[$key]['key']}` from `{$this->join[$key]['table']}` where `{$this->join[$key]['key']}`='$value'";
		$tmp = $this->db->get_one($sql);
		if($tmp)
			return true;
		else 
			return false;
	}
	function getMax_Idx()
	{
		return 0;
	}
	function Initialize_Max_Idx()
	{
		while (list($key,$value) = each($_GET)) {
			if(isset($this->struct[$key]))
				$this->struct[$key]['Form_Default'] = $value;
		}
	}
}
?>